package com.kmproject.haifu.app.utils

import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import com.bumptech.glide.request.RequestOptions
import com.jointem.yddc.utils.GlideCircleTransform
import com.kmproject.haifu.R
import com.widget.picker.bis.CustomDatePickerDialog
import com.widget.picker.bis.DataPickerDialog


fun ImageView.loadImage(url: String) {
    val options = RequestOptions()
            .centerCrop()

    Glide.with(this.context)
            .load(url)
            .apply(options)
            .into(this)
}

fun ImageView.loadImage(url: String, defImg: Int = R.drawable.default_headimage) {
    val options = RequestOptions()
            .placeholder(defImg)
            .centerCrop()

    Glide.with(this.context)
            .load(url)
            .apply(options)
            .into(this)
}

fun ImageView.loadCircleImage(url: String, defImg: Int = R.drawable.default_headimage, cct: BitmapTransformation = GlideCircleTransform()) {
    val options = RequestOptions()
            .placeholder(defImg)
            .centerCrop()
            .transform(cct)
    Glide.with(this.context)
            .asBitmap()
            .load(url)
            .apply(options)
            .into(this)
}

/**
 * 复制文字内容
 */
fun Context.copyText(str: String) {
    val cm = this.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
    val cd = ClipData.newPlainText("Label", str) as ClipData
    cm.primaryClip = cd
}

/**
 * 排序转换
 * @param data
 */
fun dataToSearch(data: String): String {
    var temSelectType = ""
    when (data) {
        "默认排序" -> {
        }
        "价格（从低到高）" -> temSelectType = "month_rent asc"
        "价格（从高到低）" -> temSelectType = "month_rent desc"
        "面积（从小到大）" -> temSelectType = "acreage asc"
        "面积（从大到小）" -> temSelectType = "acreage desc"
        "最新发布" -> temSelectType = "create_time desc"
    }
    return temSelectType
}

/**
 * 选择面积的处理{0-最小；1-最大}
 */
fun String.handleAcreage(context: Context): IntArray {
    val res = intArrayOf(0, 0)
    return when {
        this.contains(context.getString(R.string.un_limit)) -> res
        this.contains(context.getString(R.string.acreage_t_3)) -> {
            res[0] = this.substring(0, this.indexOf("m²")).toInt()
            res
        }
        this.contains(context.getString(R.string.acreage_t_2)) -> {
            res[1] = this.substring(0, this.indexOf("m²")).toInt()
            res
        }
        this.contains("-") -> {
            res[0] = this.substring(0, this.indexOf("m²")).toInt()
            res[1] = this.substring(this.indexOf("-") + 1, this.lastIndexOf("m²")).toInt()
            res
        }
        else -> res
    }
}

/**
 * 处理显示后台返回的面积
 */
fun Context.appendAcreage(lowest: Long, highest: Long): String {
    return when {
        lowest == 0L && highest == 0L -> this.getString(R.string.acreage_s_1)
        lowest == 0L && highest != 0L -> String.format(this.getString(R.string.acreage_s_2), highest)
        lowest != 0L && highest == 0L -> String.format(this.getString(R.string.acreage_s_3), lowest)
        else -> String.format(this.getString(R.string.acreage_s_4), lowest, highest)
    }
}

/**
 * 选择价格区间的处理{0-最小；1-最大}
 */
fun String.handlePrice(context: Context): IntArray {
    val res = intArrayOf(0, 0)
    val index: Int
    return when {
        this.contains(context.getString(R.string.un_limit)) -> res
        this.contains(">=") -> {//>=500
            index = this.indexOf(">=")
            res[0] = this.substring(index + 2, this.length).toInt()
            res
        }
        this.contains(">") -> {//>500
            index = this.indexOf(">")
            res[0] = this.substring(index + 1, this.length).toInt()
            res
        }
        this.contains("<=") -> {//<=500
            index = this.indexOf("<=")
            res[1] = this.substring(index + 2, this.length).toInt()
            res
        }
        this.contains("<") -> {//<500
            index = this.indexOf("<")
            res[1] = this.substring(index + 1, this.length).toInt()
            res
        }
        this.contains("-") -> {
            res[0] = this.substring(0, this.indexOf("-")).toInt()
            res[1] = this.substring(this.indexOf("-") + 1, this.length).toInt()
            res
        }
        else -> res
    }
}

/**
 * 处理电话号码，返回Array<String>,长度为2
 */
fun String.handleMobile(): Array<String> {
    val res = arrayOf("86", "")
    return when {
        this.contains("86") -> {
            res[0] = "86"
            res[1] = this.substring(this.indexOf("86") + 2, this.length)
            res
        }
        this.contains("63") -> {
            res[0] = "63"
            res[1] = this.substring(this.indexOf("63") + 2, this.length)
            res
        }
        else -> res
    }
}

fun Context.showDataPw(title: String, data: List<String>, selectionIndex: Int, listener: DataPickerDialog.OnDataSelectedListener) {
    val builder = DataPickerDialog.Builder(this)
    builder.setTitle(title)
            .setData(data)
            .setSelection(selectionIndex)
            .setOnDataSelectedListener(listener)
            .create()
            .show()
}

fun Context.showDateDialog(date: List<Int>,selectedListener:CustomDatePickerDialog.OnDateSelectedListener) {
    val builder = CustomDatePickerDialog.Builder(this)
    builder.setOnDateSelectedListener(selectedListener)
    builder.setSelectYear(date[0] - 1)
            .setSelectMonth(date[1] - 1)
            .setSelectDay(date[2] - 1)
    builder.setMinYear(date[0]).setMinMonth(date[1]).setMinDay(date[2])
    builder.create().show()
}

